<?php
/**
 * Created by PhpStorm.
 * User: 悟能科技
 * Date: 2018/12/5
 * Time: 15:17
 */

namespace app\common\controller;


use app\common\model\CardJcUserget;
use app\common\model\CardMember;
use app\common\model\CardUserGet;
use think\Controller;
use Wechat\Wxobjopen;

class CardCustom extends Wxoption
{
    //执行
    /*
     *   card_id  openid  cell_type  code
     *
     */
    public function carry_custom($param){
        try {
            $data['num'] = $this->get_custom($param);
            if ($data['num'] !== 'ERROR_EXIT') {
                $data['card_id'] = $param['card_id'];
                $data['code'] = $param['code'];
                $data['cell_type'] = $param['cell_type'];
                $res = $this->update_custom($data);
            }
        } catch (\Exception $e) {
            $res = ['errcode' => 9999, 'message' => $e->getMessage()];
        }
        return $res;
    }

    //获取更新类型及其值
    /*
     *  card_id  openid
     *
     */
    private function get_custom($param){
        try {
            $CardMember = new CardMember();
            $map['card_id'] = $param['card_id'];
            $cardinfo = $CardMember->where($map)->field('id,is_field2,custom_field2,is_field3,custom_field3')->find();
            $custom_field2 = json_decode($cardinfo['custom_field2'],true);
            $custom_field3 = json_decode($cardinfo['custom_field3'],true);
            //END
            if ($param['cell_type'] == 'two_cell' && $param['is_field2'] == 1){
                $name = $custom_field2['name'];
            }elseif ($param['cell_type'] == 'three_cell' && $param['is_field3'] == 1){
                $name = $custom_field3['name'];
            }else{
                $name = '';
            }
            //END
            switch ($name){
                case "余额":
                    $time = time();
                    $sql['openid'] = $param['openid'];
                    $sql['card_type'] = ['neq','MEMBER_CARD'];
                    $sql['statu'] = ['neq',2];
                    $sql['have'] = 1;
                    $sql['end_time'] = ['ELT',$time];
                    $CardUserGet = new CardUserGet();
                    $number = $CardUserGet->where($sql)->value('balance') . '元';
                    break;
                case "优惠券":
                    $time = time();
                    $sql['openid'] = $param['openid'];
                    $sql['card_type'] = ['neq','MEMBER_CARD'];
                    $sql['statu'] = ['neq',2];
                    $sql['have'] = 1;
                    $sql['end_time'] = ['ELT',$time];
                    $CardUserGet = new CardUserGet();
                    $number = $CardUserGet->where($sql)->count('id') . '张';
                    break;
                case "次数":
                    $sql['openid'] = $param['openid'];
                    $sql['code'] = $param['code'];
                    $sql['is_del'] = 1;
                    $CardJcUserget = new CardJcUserget();
                    $number = $CardJcUserget->where($sql)->sum('surplus_num') . '次';
                    break;
                default:
                    $number = 'ERROR_EXIT';
                    break;
            }
        } catch (\Exception $e) {
            $number = $e->getMessage();
        }
        return $number;
    }

    //更新卡面
    private function update_custom($param){
        try {
            //实例化微信
            $options = $this->options($param['card_id']);
            $WxObj = new Wxobjopen($options);
            $data = [
                'code' => $param['code'],
                'card_id' => $param['card_id'],
                'notify_optional' => [
                    'is_notify_bonus' => false,
                    'is_notify_balance' => false,
                ],
            ];
            //END
            if ($param['cell_type'] == 'two_cell'){
                $data['custom_field_value2'] = $param['num'];
                $data['notify_optional']['is_notify_custom_field2'] = true;
            }elseif ($param['cell_type'] == 'three_cell'){
                $data['custom_field_value3'] = $param['num'];
                $data['notify_optional']['is_notify_custom_field3'] = true;
            }else{
                $name = '';
            }
            //END
            if (!empty($arr['background_pic_url'])) {
                $data['background_pic_url'] = $arr['background_pic_url'];
            }
            //END
            $res = $WxObj->updateUserInfo($data);
        } catch (\Exception $e) {
            $res = ['errcode' => 9999, 'message' => $e->getMessage()];
        }
        return $res;
    }

}